package com.hangzhou.xyj.ui.widget;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;

import com.hangzhou.xyj.XYJApplication;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.net.handler.AddFollowHandler;
import com.hangzhou.xyj.net.handler.CancelFollowHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.listener.HttpResponseListener;
import com.hangzhou.xyj.net.request.AddFollowReq;
import com.hangzhou.xyj.net.request.CancelFollowReq;
import com.hangzhou.xyj.ui.activity.LoginActivity;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.dialog.TipsDialog;
import com.hangzhou.xyj.utils.StringUtil;
import com.hangzhou.xyj.utils.ToastUtil;

/**
 * @author : neo.duan
 * @date : 	 2016/3/3
 * @desc : 自定义公共的关注按钮
 */
public class CommonFollowButton extends Button implements View.OnClickListener {
    public int mFollowId; //被关注人的id
    public boolean mIsFollow; //是否已关注
    private Context mContext;
    private int mUserId;

    public CommonFollowButton(Context context) {
        this(context, null);
    }

    public CommonFollowButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CommonFollowButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initViews();
    }


    public void initViews() {

//        setBackgroundResource(R.drawable.selector_btn_small_yellow);
//        setMinWidth(mContext.getResources().getDimensionPixelSize(R.dimen.follow_btn_width));
//        setMinHeight(mContext.getResources().getDimensionPixelSize(R.dimen.follow_btn_height));
//        setTextColor(mContext.getResources().getColor(R.color.common_white));
//        setTextSize(mContext.getResources().getDimension(R.dimen.follow_btn_text_size));
        setGravity(Gravity.CENTER);

        setOnClickListener(this);
    }

    public void setFollow(boolean isFollow, int followId) {
        this.mIsFollow = isFollow;
        this.mFollowId = followId;
        if (isFollow) {
            this.setVisibility(GONE);
        } else {
            this.setVisibility(VISIBLE);
        }
    }

    /**
     * 更新View：如果已关注则隐藏按钮
     */
    public void updateView() {
        if (mIsFollow) {
            this.setText("已关注");
        } else {
            this.setText("关注");
        }
    }

    /**
     * 点击事件：已关注则取消关注，未关注则添加关注
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        //没有登录
        if (XYJApplication.getUserInfo() != null) {
            String userId;
            if (StringUtil.isNotEmpty(userId = XYJApplication.getUserInfo().getUserId()))
                mUserId = Integer.valueOf(userId);
        }

        //用户未登陆
        if (mUserId == 0) {
            TipsDialog dialog = new TipsDialog(mContext, "提示", "需要登录才能操作，是否登录？", "确定", "取消") {
                @Override
                protected void OnClickNegativeButton() {
                    this.dismiss();
                }

                @Override
                protected boolean OnClickPositiveButton() {
                    //跳转到我的动态
                    IntentManager.getInstance().goActivity(mContext, new Intent(mContext, LoginActivity.class));
//                    ScreenManager.getInstance().currentActivity().finish();
                    return true;
                }

            };
            dialog.show();
            return;
        }
        if (mIsFollow) {
            cancelFollow();
        } else {
            addFollow();
        }
    }

    /**
     * 添加关注
     */
    private void addFollow() {
        AddFollowReq req = new AddFollowReq(AddFollowReq.TYPE_USER, mFollowId, mUserId);
        HttpLoader.getInstance().sendRequest(new AddFollowHandler(req, new HttpResponseListener() {
            @Override
            public void onStart() {
                showLoadingDialog();
            }

            @Override
            public void onSuccess(Object model) {
                hideLoadingDialog();
                if ((Boolean) model) {
                    //关注成功
                    ToastUtil.show("关注成功");
                    mIsFollow = true;
                    updateView();
                } else {
                    //关注失败
                    ToastUtil.show("关注失败");
                }
            }

            @Override
            public void onFail(String msg) {
                hideLoadingDialog();
                ToastUtil.show(msg);
            }

            @Override
            public void onDone(String msg) {
                hideLoadingDialog();
                ToastUtil.show(msg);
            }

            @Override
            public void onError() {
                hideLoadingDialog();
                ToastUtil.show("网络错误");
            }
        }, 0));
    }

    /**
     * 取消关注
     */
    private void cancelFollow() {
        CancelFollowReq req = new CancelFollowReq(CancelFollowReq.TYPE_USER, mFollowId, mUserId);
        HttpLoader.getInstance().sendRequest(new CancelFollowHandler(req, new HttpResponseListener() {
            @Override
            public void onStart() {
                showLoadingDialog();
            }

            @Override
            public void onSuccess(Object model) {
                hideLoadingDialog();
                if ((Boolean) model) {
                    //取消关注成功
                    ToastUtil.show("取消关注成功");
                    mIsFollow = false;
                    updateView();
                } else {
                    //取消关注失败
                    ToastUtil.show("取消关注失败");
                }
            }

            @Override
            public void onFail(String msg) {
                hideLoadingDialog();
                ToastUtil.show(msg);
            }

            @Override
            public void onDone(String msg) {
                hideLoadingDialog();
                ToastUtil.show(msg);
            }

            @Override
            public void onError() {
                hideLoadingDialog();
                ToastUtil.show("网络错误");
            }
        }, 0));
    }

    /**
     * 显示加载框
     */
    private void showLoadingDialog() {
        if (mContext instanceof BaseActivity) {
            ((BaseActivity) mContext).showLoadingDialog();
        }
    }

    /**
     * 隐藏加载框
     */
    private void hideLoadingDialog() {
        if (mContext instanceof BaseActivity) {
            ((BaseActivity) mContext).hideLoadingDialog();
        }
    }
}
